package main.java.utils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

public class Utils {

  public Utils() {
  }

  /**
   * Give a representation of a TreeMap
   * 
   * @param tree
   *          the TreeMap to show
   * @return a represantation of the TreeMap
   */
  public static String treeMapToString(final TreeMap<?, ?> tree) {
    List<String> ret = new ArrayList<String>();
    for (Map.Entry<?, ?> entry : tree.entrySet()) {
      ret.add(entry.getKey() + " : " + entry.getValue());
    }

    return Utils.join(ret, "\n");
  }

  /**
   * From org.apache.commons
   * 
   * <p>
   * Joins the elements of the provided <code>Iterator</code> into a single
   * String containing the provided elements.
   * </p>
   * 
   * <p>
   * No delimiter is added before or after the list. A <code>null</code>
   * separator is the same as an empty String ("").
   * </p>
   * 
   * <p>
   * See the examples here: {@link #join(Object[],String)}.
   * </p>
   * 
   * @param iterator
   *          the <code>Iterator</code> of values to join together, may be null
   * @param separator
   *          the separator character to use, null treated as ""
   * @return the joined String, <code>null</code> if null iterator input
   */
  public static String join(Iterator iterator, String separator) {

    // handle null, zero and one elements before building a buffer
    if (iterator == null) {
      return null;
    }
    if (!iterator.hasNext()) {
      return "";
    }
    Object first = iterator.next();
    if (!iterator.hasNext()) {
      return first == null ? "" : first.toString();
    }

    // two or more elements
    StringBuilder buf = new StringBuilder(256); // Java default is 16, probably
                                                // too small
    if (first != null) {
      buf.append(first);
    }

    while (iterator.hasNext()) {
      if (separator != null) {
        buf.append(separator);
      }
      Object obj = iterator.next();
      if (obj != null) {
        buf.append(obj);
      }
    }
    return buf.toString();
  }
  
  /**
   * <p>Joins the elements of the provided <code>Collection</code> into
   * a single String containing the provided elements.</p>
   *
   * <p>No delimiter is added before or after the list. Null objects or empty
   * strings within the iteration are represented by empty strings.</p>
   *
   * <p>See the examples here: {@link #join(Object[],char)}. </p>
   *
   * @param collection  the <code>Collection</code> of values to join together, may be null
   * @param separator  the separator character to use
   * @return the joined String, <code>null</code> if null iterator input
   * @since 2.3
   */
  public static String join(Collection collection, char separator) {
      if (collection == null) {
          return null;
      }
      return join(collection.iterator(), ""+separator);
  }
  
  /**
   * <p>Joins the elements of the provided <code>Collection</code> into
   * a single String containing the provided elements.</p>
   *
   * <p>No delimiter is added before or after the list.
   * A <code>null</code> separator is the same as an empty String ("").</p>
   *
   * <p>See the examples here: {@link #join(Object[],String)}. </p>
   *
   * @param collection  the <code>Collection</code> of values to join together, may be null
   * @param separator  the separator character to use, null treated as ""
   * @return the joined String, <code>null</code> if null iterator input
   * @since 2.3
   */
  public static String join(Collection collection, String separator) {
      if (collection == null) {
          return null;
      }
      return join(collection.iterator(), separator);
  }  
}
